package Thinh.thinh.dao;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.mapping.PersistentClass;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


public abstract class GenericHibernateDAO <T, ID extends Serializable> extends HibernateDaoSupport
		 {

	 private Class<T> persistentClass; 
	
	
	protected GenericHibernateDAO(Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }
   




	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public T findById(ID id) {
		T entity=(T)getHibernateTemplate().get( persistentClass, id);
		
		return entity;
	}
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<T> findAll() {
		
		System.out.print(persistentClass);
		return (List<T>)  getHibernateTemplate().find("from "+persistentClass.getName());
	}


	@Transactional
	public T create(T entity) {
		
		return (T) getHibernateTemplate().save(entity);
	}

	@Transactional
	public void update(T entity) {
	 getHibernateTemplate().saveOrUpdate(entity);
		
	}
	@Transactional
	public void delete(T entity) {
		 getHibernateTemplate().delete(entity);
	}

	}
